<?php
/**
 * Created by PhpStorm.
 * User: Jessewang
 * Date: 2017/3/29
 * Time: 上午11:34
 */

namespace App\Libs;
require_once dirname(__FILE__)."/PhpServerSdk/TLSSigAPI.php";
require_once dirname(__FILE__)."/PhpServerSdk/TimRestApi.php";
use Swoole;
use Swoole\Client\Http;
class TimApi
{


    public  $appid=null;
    public  $appkey = null;
    public $indetifier = null;
    public  $api = null;
    public $timapi = null;
    function  __construct()
    {
        $config = \Swoole::$php->config["sns"];

        $this->appid = $config['tim']['appid'];
        $this->appkey = $config['tim']['appkey'];
        $this->indetifier = $config['tim']['indetifier'];
        $this->api = new \TLSSigAPI();
        $this->api->setAppid($this->appid);

        $this->timapi = createRestAPI();
        $this->timapi->init($this->appid,$this->indetifier );


        $private = file_get_contents(dirname(__FILE__)."/PhpServerSdk/keys/".'private_key');
        $this->api->SetPrivateKey($private);
        $public = file_get_contents(dirname(__FILE__)."/PhpServerSdk/keys/".'public_key');
        $this->api->SetPublicKey($public);
       // $adminSig = $this->genSig($this->indetifier);
        $adminSig = $config['tim']['adminsig'];
       // var_dump($adminSig);
        $this->timapi->set_user_sig($adminSig);
    }

    function genSig($userid)
    {
        $sig = $this->api->genSig($userid);
        return $sig;
    }
    function  verifySig($sig,$userid)
    {
        return $result = $this->api->verifySig($sig, $userid, $init_time, $expire_time, $error_msg);
    }

    function reg_account($userinfo)
    {
        $ret = $this->timapi->account_import((string)$userinfo['user_id'],$userinfo['user_name'],$userinfo['user_head_img']);

    }
    function setPorfile($userid,$profile)
    {
        //var_dump("111111111");
        $profilelist = array(
            array(
            "Tag"=>"Tag_Profile_IM_Nick",
            "Value"=>$profile['user_name']
            ),
            array(
                "Tag"=>"Tag_Profile_IM_Gender",
                "Value"=> $profile['user_sex']=="1" ? "Gender_Type_Male":"Gender_Type_Female"
            ),
            array(
                "Tag"=>"Tag_Profile_IM_Image",
                "Value"=> $profile['user_head_img']
            )
        );

        $ret = $this->timapi->profile_portrait_set2((string)$userid,$profilelist);

    }
    function sendMsg($srcuser,$dstuser,$msgbody)
    {
        return $this->timapi->openim_send_msg3($srcuser,$dstuser,$msgbody);
    }
    function  addFriend($srcuser,$destuser)
    {
        return $this->timapi->sns_friend_import($srcuser,$destuser);
    }
}